package org.yylnb.openplatform.monitor.listener;

import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.yylnb.openplatform.monitor.event.EventBean;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;


/**
 * @author RainLin
 * @date 2020/8/3 - 19:18
 */
@Component
public class MailEventListener {

    /**
     * 监听事件,发送警告邮件
     *
     * @param bean
     */
    @EventListener
    public void onEvent(EventBean bean) {
        switch (bean.getEventType()) {

            case OVERTIME:

                try {
                    Properties properties = new Properties();

                    properties.put("mail.host", "smtp.qq.com");
                    //设置协议
                    properties.put("mail.transport.protocol", "smtp");
                    //代表需要认证
                    properties.put("mail.smtp.auth", "true");
                    //创建ssl链接的工厂
                    MailSSLSocketFactory factory = new MailSSLSocketFactory();
                    factory.setTrustAllHosts(true);
                    //设置开启ssl
                    properties.put("mail.smtp.ssl.enable", "true");
                    //设置ssl的工厂
                    properties.put("mail.smtp.ssl.socketFactory", factory);

                    Session session = Session.getDefaultInstance(properties, new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("5433708@qq.com", "akaodyzjoxngbgeg");
                        }
                    });
                    //主要目的获取日志
                    session.setDebug(true);
                    //类似于sql中的statement
                    Transport transport = session.getTransport();
                    transport.connect("smtp.qq.com", "5433708@qq.com", "akaodyzjoxngbgeg");

                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("5433708@qq.com"));
                    message.setSubject("超时告警邮件");
                    message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("571217087@qq.com"), new InternetAddress("1216092421@qq.com")});
                    message.setContent(bean.getMsg(), "text/html;charset=utf-8");
                    transport.sendMessage(message, message.getAllRecipients());
                    transport.close();
                } catch (GeneralSecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchProviderException e) {
                    e.printStackTrace();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }


                break;
            default:
                break;
        }

    }
}

